import datetime
import database


def book_return(book_id):
    status = database.book_status(book_id)
    # if book is available already
    if status[1] == "0":
        return "0"
    else:
        # get return time
        return_date = datetime.datetime.now()
        # get the transaction_id of check out this book
        transaction_id = database.get_transaction_id(book_id)
        # check the book return late or not
        checkout_date = datetime.datetime.strptime(database.get_checkout_date(transaction_id), "%Y-%m-%d")
        days = (return_date - checkout_date).days
        if days > status[0]:
            # return the book
            database.book_return(book_id)
            # update the loan history
            database.update_loan_history(transaction_id, return_date)
            return "1"
        elif days == status[0]:
            # return the book
            database.book_return(book_id)
            # update the loan history
            database.update_loan_history(transaction_id, return_date)
            return "2"
        else:
            # return the book
            database.book_return(book_id)
            # update the loan history
            database.update_loan_history(transaction_id, return_date)
            return "2"


if __name__ == "__main__":
    # test data
    book_return("10")
